// SPDX-License-Identifier: GPL-2.0
/*
 * ARM callchain support
 *
 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
 *
 * This code is based on the ARM OProfile backtrace code.
 */
#include <linux/perf_event.h>
#include <linux/uaccess.h>

#include <asm/stacktrace.h>

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
#include <linux/rtos_perf_user_backtrace.h>
#endif

/*
 * The registers we're interested in are at the end of the variable
 * length saved register structure. The fp points at the end of this
 * structure so the address of this struct is:
 * (struct frame_tail *)(xxx->fp)-1
 *
 * This code has been adapted from the ARM OProfile support.
 *
 * CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
 * Compiler usually optimize stack behavior and the backtrace method described
 * above only work when compiler option -mapcs-frame is set. With this config,
 * the backtrace method is improved with -fno-omit-frame-pointer. When this
 * compiler option is used, the fp points to the lr in the previou
 * frame, and the previous fp is the pointer in front of the founded lr.
 */
struct frame_tail {
	struct frame_tail __user *fp;
	unsigned long sp;
	unsigned long lr;
} __attribute__((packed));

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
#define FRAME_LEN (sizeof(struct frame_tail) - sizeof(unsigned long))
#endif

/*
 * Get the return address for a single stackframe and return a pointer to the
 * next frame tail.
 */
static struct frame_tail __user *
user_backtrace(struct frame_tail __user *tail,
	       struct perf_callchain_entry_ctx *entry)
{
	struct frame_tail buftail;
	unsigned long err;

	if (!access_ok(tail, sizeof(buftail)))
		return NULL;

	pagefault_disable();
	err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
	pagefault_enable();

	if (err)
		return NULL;

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
	/*
	 * When -fno-omit-frame-pointer compiler option is used, the sp pointer
	 * will be remove in frame. But To be compatible with both -mapcs-frame
	 * and -fno-omit-frame-pointer options, we had not removed sp pointer in
	 * struct a32_frame_tail. So when we copy frame into struct a32_frame_tail
	 * from user space if -fno-omit-frame-pointer is used, the real lr pointer
	 * will be put in member sp.
	 */
	if (improve_perf_user_backtrace_enable == 1)
		perf_callchain_store(entry, buftail.sp);
	else
		perf_callchain_store(entry, buftail.lr);
#else
	perf_callchain_store(entry, buftail.lr);
#endif
	/*
	 * Frame pointers should strictly progress back up the stack
	 * (towards higher addresses).
	 */
#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
	if (improve_perf_user_backtrace_enable == 1) {
		if ((void *)tail + FRAME_LEN >= (void *)buftail.fp)
			return NULL;
	} else {
		if (tail + 1 >= buftail.fp)
			return NULL;
	}
#else
	if (tail + 1 >= buftail.fp)
		return NULL;
#endif

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
	if (improve_perf_user_backtrace_enable == 1)
		/* Previous fp is stored at 4 bytes before current fp points to */
		return (struct frame_tail *)((unsigned long)buftail.fp - 4);
#endif
	return buftail.fp - 1;
}

void
perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
{
	struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
	struct frame_tail __user *tail;

	if (guest_cbs && guest_cbs->is_in_guest()) {
		/* We don't support guest os callchain now */
		return;
	}

	perf_callchain_store(entry, regs->ARM_pc);

	if (!current->mm)
		return;

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
	read_lock(&improve_perf_user_backtrace_lock);
	if (improve_perf_user_backtrace_enable == 1)
		/* Previous fp is stored at 4 bytes before current fp points to */
		tail = (struct frame_tail __user *)(regs->ARM_fp - 4);
	else
		tail = (struct frame_tail __user *)regs->ARM_fp - 1;
#else
	tail = (struct frame_tail __user *)regs->ARM_fp - 1;
#endif

	while ((entry->nr < entry->max_stack) &&
	       tail && !((unsigned long)tail & 0x3))
		tail = user_backtrace(tail, entry);

#ifdef CONFIG_RTOS_IMPROVE_PERF_USER_BACKTRACE
	read_unlock(&improve_perf_user_backtrace_lock);
#endif
}

/*
 * Gets called by walk_stackframe() for every stackframe. This will be called
 * whist unwinding the stackframe and is like a subroutine return so we use
 * the PC.
 */
static int
callchain_trace(struct stackframe *fr,
		void *data)
{
	struct perf_callchain_entry_ctx *entry = data;
	perf_callchain_store(entry, fr->pc);
	return 0;
}

void
perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
{
	struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
	struct stackframe fr;

	if (guest_cbs && guest_cbs->is_in_guest()) {
		/* We don't support guest os callchain now */
		return;
	}

	arm_get_current_stackframe(regs, &fr);
	walk_stackframe(&fr, callchain_trace, entry);
}

unsigned long perf_instruction_pointer(struct pt_regs *regs)
{
	struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();

	if (guest_cbs && guest_cbs->is_in_guest())
		return guest_cbs->get_guest_ip();

	return instruction_pointer(regs);
}

unsigned long perf_misc_flags(struct pt_regs *regs)
{
	struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
	int misc = 0;

	if (guest_cbs && guest_cbs->is_in_guest()) {
		if (guest_cbs->is_user_mode())
			misc |= PERF_RECORD_MISC_GUEST_USER;
		else
			misc |= PERF_RECORD_MISC_GUEST_KERNEL;
	} else {
		if (user_mode(regs))
			misc |= PERF_RECORD_MISC_USER;
		else
			misc |= PERF_RECORD_MISC_KERNEL;
	}

	return misc;
}
